package com.demo.service;

import com.demo.model.db1.WfStaffDAO;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.ibatis.solon.annotation.Db;
import org.noear.solon.annotation.Inject;
import org.noear.solon.aspect.annotation.Service;
import org.noear.solon.data.annotation.Tran;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class FlowTestService {

    @Inject
    RepositoryService repositoryService;

    @Inject
    RuntimeService runtimeService;

    @Db("db1")
    @Inject
    WfStaffDAO wfStaffDAO;

    //模板列表
    public List<ProcessDefinition> getTemplateList() throws Exception {
        return repositoryService.createProcessDefinitionQuery().list();
    }


    //测试成功发起流程
    @Tran
    public void testStartFlowSuccess() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        runtimeService.startProcessInstanceByKey("hardship_apply", "test_solon_" + sdf.format(new Date()));
    }

    //测试错误回滚
    @Tran
    public void testStartFlowError() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        runtimeService.startProcessInstanceByKey("hardship_apply", "test_solon_" + sdf.format(new Date()));
        wfStaffDAO.testInsert();
        throw new RuntimeException("阻止发起");
    }

}
